import 'package:app/pages/daily_work_report_module/my_daily_detail_module/my_daily_detail_view.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import '../../../services/address.dart';
import '../../../services/dio_manager.dart';
import '../top_dialog_page.dart';
import 'my_mission_model.dart';

class MyMissionController extends GetxController {
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestData(reply_type: replyTypeId);
  }

  EasyRefreshController easyRefreshController = EasyRefreshController();

  TextEditingController editingController = TextEditingController();

  bool isTopShow = false;

  String replyType = '我创建的';
  String replyTypeId = 'create';

  var page = 1;

  var dataArr = [];

  List<MyTaskItemModel>? missionList;

  /// 获取我的任务列表数据
  requestData({var reply_type, var search}) async {
    var params = {
      'task_type': '${reply_type}',
      'page': page,
      'limit': '10',
    };
    var json = await DioManager()
        .kkRequest(Address.missionList, params: params, method: 'get');

    if (json["errcode"] == 0) {
      missionList = <MyTaskItemModel>[];
      if(json['data']['list']!=null){
        json['data']["list"].forEach((v) {
          missionList!.add(new MyTaskItemModel.fromJson(v));
        });
      }
    }

    if (page == 1) {
      easyRefreshController.resetLoadState();
      dataArr.clear();
      dataArr.addAll(missionList!);
    } else {
      dataArr.addAll(missionList!);
      if (missionList == null) {
        easyRefreshController.finishLoad(noMore: true);
        BotToast.showText(text: '暂无更多');
      }
    }
    update();
  }

  /// 点击的弹框
  topClick() async {
    isTopShow = true;

    var a;
    if (replyType == '我创建的') {
      a = 0;
    }
    if (replyType == '我参与的') {
      a = 1;
    }
    if (replyType == '我关注的') {
      a = 2;
    }
    if(replyType == '已完成'){
      a = 3;
    }

    var data =
        await Get.dialog(TopDialogPage(a), barrierColor: Colors.transparent);
    if (data != null) {
      print(data);
      isTopShow = data['isShow'];
      replyType = data['text'];
      replyTypeId = data['reply_type'];
      requestData(reply_type: replyTypeId);
    }
    update();
  }

  ///下拉刷新
  pullToData() async {
    page = 1;
    requestData(reply_type: replyTypeId);
  }

  /// 上拉加载
  onLoadToData() async {
    page++;
    requestData(reply_type: replyTypeId);
  }

  /// 跳转页面
  pushToView(int index) async {
    MyTaskItemModel model = dataArr[index];
    var data = await Get.toNamed('/myMissionDetailDetail', arguments: model.id);
    if (data == 'refresh') {
      requestData(reply_type: 'create');
    }
  }
}
